const fp = require("lodash/fp");
const { Maybe, Container } = require("./support");

//练习1: 让functor 里的值增加的函数ex1 实现
let maybe = Maybe.of([5, 6, 1]);
let ex1 = (addNum) => {
  //实现
  let arr = maybe._value;
  let result = fp.map((x) => fp.add(x, addNum), arr);
  return (maybe = Maybe.of(result));
};

console.log("练习1: 让functor 里的值增加的函数ex1 实现");
console.log(ex1(4));

//练习2  fp.first()获取第一个元素
let xs = Container.of(["do", "ray", "me", "fa", "so", "la", "ti", "do"]);
let ex2 = () => {
  return fp.first(xs._value);
};
console.log("练习2  fp.first()获取第一个元素");
console.log(ex2());

//练习3 safeProp()  fp.first()获取user的名字的首字母
let safeProp = fp.curry(function (x, o) {
  return Maybe.of(o[x]);
});
let user = { id: 2, name: "Albert" };
let ex3 = (value) => {
  return fp.first(value);
};
console.log("练习3 safeProp()  fp.first()获取user的名字的首字母");
console.log(ex3(safeProp("name", user)._value));

//练习4 使用maybe 重写ex4
let ex4 = function (n) {
  return Maybe.of(n).map(parseInt)._value;
  //   if (n) {
  //     return parseInt(n);
  //   }
};
console.log("练习4 使用maybe 重写ex4");
console.log(ex4("200"));
console.log(ex4("a"));
